#include <iostream>
#include <memory>
#include <string>

using namespace std;

class MyClass {
 string message;
 public:
 MyClass() {
   message = "делаю что-то";
 }
 void DoSomething() {
  cout << message << endl;
 }
};

int main() {
 cout << "--без auto_ptr--" << endl;
 try {
  MyClass* q;
  {
  MyClass* p(new MyClass);
  q = p;
//  MyClass* q = p;
//  delete p;
  p->DoSomething();   // Watch out! p is now dangling!
  p = NULL;           // p is no longer dangling
  }
  q->DoSomething();   // Ouch! q is still dangling!
//  cout << p << "\t" << q << endl;
 } catch(...) {
  cout << "exception!" << endl;
 }
 cout << "--с использованием auto_ptr--" << endl;
 try {
  auto_ptr<MyClass> t;
  {
  auto_ptr<MyClass> s(new MyClass);
//  auto_ptr<MyClass> t(s);
  t = s;
//  delete s;
  s->DoSomething();
  }
//  s = NULL;
  t->DoSomething();
//  cout << s << "\t" << &t << endl;
 } catch(...) {
  cout << "exception!" << endl;
 }
 return 0;
}
